package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.CreditAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.db.CurUserModel;
import com.kkd.kkdapp.db.app.model.User;
import com.kkd.kkdapp.http.entity.CrediItem;
import com.kkd.kkdapp.http.entity.Credit;
import com.kkd.kkdapp.http.entity.CreditData;
import com.kkd.kkdapp.http.entity.ManageDataList;
import com.kkd.kkdapp.http.manager.HttpCallBack;

import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;
import com.kkd.kkdapp.utils.Time_Date;

import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

public class MyCreditActivity extends BaseActivity implements OnClickListener,
		KKDScrollListener {
	private static final int NOMORE = 0;
	private static final int REFRESH = 1;
	private static final int REFRESH_ONEPAGE = 2;
	private RelativeLayout credit_top_img;
	private Context mContext = this;
	private TextView credit_tv_null;
	private ImageView credit_img_null;
	private KkdXlistView credit_lv;
	private ListView mListView;
	private CreditAdapter adapter;
	private TextView howto;
	private boolean REFRESHFLAG = true;
	private long pageNO = 1;
	private long pageSize = 10;
	private ArrayList<CrediItem> datalist = new ArrayList<CrediItem>();
	private Dialog mDialog;
	// 状态变为还清
	public static String flag_bidId_changeToPayOff = "";

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case NOMORE:
				credit_lv.stopLoadMore(-1);
				credit_lv.stopRefresh();
				break;
			case REFRESH:
				if (datalist.size() > 0) {
					adapter.notifyDataSetChanged();
					credit_lv.stopLoadMore(0);
					credit_lv.stopRefresh();
					Time_Date time_Date = new Time_Date();
					String time = time_Date.get_Year_Day();
					credit_lv.setRefreshTime(time);

				} else {
					credit_img_null.setVisibility(View.VISIBLE);
					credit_tv_null.setVisibility(View.VISIBLE);
					credit_lv.stopLoadMore(0);
					credit_lv.stopRefresh();
					Time_Date time_Date = new Time_Date();
					String time = time_Date.get_Year_Day();
					credit_lv.setRefreshTime(time);
				}
				break;
			case REFRESH_ONEPAGE:
				if (datalist.size() > 0) {
					adapter.notifyDataSetChanged();
					credit_lv.stopLoadMore(1);
					credit_lv.stopRefresh();
					Time_Date time_Date = new Time_Date();
					String time = time_Date.get_Year_Day();
					credit_lv.setRefreshTime(time);

				} else {
					credit_img_null.setVisibility(View.VISIBLE);
					credit_tv_null.setVisibility(View.VISIBLE);
					credit_lv.stopLoadMore(0);
					credit_lv.stopRefresh();
					Time_Date time_Date = new Time_Date();
					String time = time_Date.get_Year_Day();
					credit_lv.setRefreshTime(time);
				}
				break;
			case 3:
				mHandler.postDelayed(new Runnable() {

					@Override
					public void run() {
						showToast("加载失败");
						credit_lv.stopLoadMore(1);
						credit_lv.stopRefresh();
						if (datalist.size() == 0) {
							credit_img_null.setVisibility(View.VISIBLE);
							credit_tv_null.setVisibility(View.VISIBLE);
							// credit_tv_null.setText("加载失败");
						}
					}
				}, 0);
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_credit_activity);
		initView();
		setListener();
		loadData();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kkd.kkdapp.base.BaseActivity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		// onRefresh();
		if (!TextUtils.isEmpty(flag_bidId_changeToPayOff)) {
			for (int i = 0; i < datalist.size(); i++) {
				if (datalist.get(i).getBidId()
						.equals(flag_bidId_changeToPayOff)) {
					datalist.get(i).setStatus("5005");
					adapter.notifyDataSetChanged();
					flag_bidId_changeToPayOff = "";
					break;
				}
			}
		}
	}

	private void loadData() {
		credit_lv.startRefresh();
		if (AppUtil.isConnect(mContext)) {
			if (datalist == null || datalist.size() == 0) {
				// credit_img_null.setVisibility(View.VISIBLE);
				credit_img_null.setImageResource(R.drawable.img_null);
				// credit_tv_null.setVisibility(View.VISIBLE);
				credit_tv_null.setText("暂无借款记录");
			} else {
				credit_img_null.setVisibility(View.GONE);
				credit_tv_null.setVisibility(View.GONE);
			}
			requestCreditDetails();
		} else {
			if (datalist == null || datalist.size() == 0) {
				credit_img_null.setVisibility(View.VISIBLE);
				credit_img_null.setImageResource(R.drawable.img_null);
				credit_tv_null.setVisibility(View.VISIBLE);
				credit_tv_null.setText("网络连接失败！");
			} else {
				credit_img_null.setVisibility(View.GONE);
				credit_tv_null.setVisibility(View.GONE);
			}
			credit_lv.stopRefresh();
		}
	}

	private void setListener() {
		credit_top_img.setOnClickListener(this);
		howto.setOnClickListener(this);
	}

	private void initView() {
		credit_top_img = (RelativeLayout) findViewById(R.id.credit_top_img);
		credit_tv_null = (TextView) findViewById(R.id.credit_tv_null);
		credit_img_null = (ImageView) findViewById(R.id.credit_img_null);
		credit_lv = (KkdXlistView) findViewById(R.id.credit_lv);
		credit_lv.setBottomTip("借款记录已经全部展示");
		howto = (TextView) findViewById(R.id.tv_howto);
		credit_lv.setKKDScrollListener(this);
		mListView = credit_lv.mListView;
		mListView.setDividerHeight(0);
		adapter = new CreditAdapter(this, datalist);
		mListView.setAdapter(adapter);
		credit_tv_null.setVisibility(View.GONE);
		credit_img_null.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.credit_top_img:
			finish();
			break;
		case R.id.tv_howto:
			startActivity(new Intent(mContext, HowToRepaymentActivity.class));
			break;

		default:
			break;
		}
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		requestCreditDetails();
		if (AppUtil.isConnect(mContext)) {
			if (datalist == null) {
				credit_img_null.setVisibility(View.VISIBLE);
				credit_img_null.setImageResource(R.drawable.img_null);
				credit_tv_null.setVisibility(View.VISIBLE);
				credit_tv_null.setText("暂无借款记录");
			} else {
				// credit_img_null.setVisibility(View.GONE);
				// credit_tv_null.setVisibility(View.GONE);
				REFRESHFLAG = true;
			}
		} else {
			if (datalist == null || datalist.size() == 0) {
				credit_img_null.setVisibility(View.VISIBLE);
				credit_img_null.setImageResource(R.drawable.img_null);
				credit_tv_null.setVisibility(View.VISIBLE);
				credit_tv_null.setText("网络连接失败！");
			} else {
				credit_img_null.setVisibility(View.GONE);
				credit_tv_null.setVisibility(View.GONE);
			}
		}
	}

	@Override
	public void onLoadMore() {
		if (AppUtil.isConnect(mContext)) {
			pageNO++;
			requestCreditDetails();
			REFRESHFLAG = false;
		} else {
			showToast("网络连接失败！");
			credit_lv.stopLoadMore(0);
		}
	}

	public void requestCreditDetails() {
		KkdHttpMagager.getInstance().getCreditManager(pageNO, pageSize,
				CreditData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						CreditData data = (CreditData) responseBody;
						long pageCount = data.getPageCount();
						long pageNo = data.getPageNO();
						if (pageNo > pageCount) { // 已经加载完毕
							mHandler.sendEmptyMessage(NOMORE);
						} else {
							if (pageNO == 1) {
								datalist.clear();
								credit_lv.setNoMore(false);
							}
							datalist.addAll(data.getData());
							if (data.getData().size() < pageSize) {
								mHandler.sendEmptyMessage(REFRESH_ONEPAGE);
							} else {
								mHandler.sendEmptyMessage(REFRESH);
							}
						}
						if (REFRESHFLAG) {
							if (datalist == null || datalist.size() == 0) {
								credit_img_null.setVisibility(View.VISIBLE);
								credit_tv_null.setVisibility(View.VISIBLE);
							} else {
								credit_img_null.setVisibility(View.GONE);
								credit_tv_null.setVisibility(View.GONE);
							}
						}
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						mHandler.sendEmptyMessage(3);
					}
				});
	}

	// 如何还款
	public void getHowRepayment() {
		if (mDialog != null) {
			mDialog.cancel();
			mDialog = null;
		}
		mDialog = DialogUtils.howRepayment(this, new OnClickListener() {

			@Override
			public void onClick(View v) {
				mDialog.dismiss();
			}
		});
	}
}
